/*=========================================================================
Elite College as Engines of Upward Mobility: Evidence from Colombia's Ser Pilo Paga
Authors: Juliana Londoño-Vélez, Catherine Rodriguez, Fabio Sánchez
and Luis Esteban Álvarez-Arango

Creation date: June 6th, 2025
--------------------------------------------------------------------------
Figures A12a and A12b
=========================================================================*/
        
use "${Intermediate}/Data_Saber11_SNIES.dta", clear
		  
tab icfes_per tipo
fasterxtile pc=running_saber11, nq(100) by(icfes_per)   // mata: if fasterxtile fails, run mata mlib index before 
replace pc=. if access_0==0
drop if codigo_ies==.
destring icfes_per, replace

collapse pc (sum) access_0, by(codigo_ies icfes_per tipo)
replace icfes_per=round(icfes_per/10)
gen borrar=(access_0!=0)
bysort codigo_ies: ereplace borrar=total(borrar)
keep if borrar==6
drop borrar
		
gen ies_2013_aux = access_0 if icfes_per==2013 
bys codigo_ies : egen stut_2013 = max(ies_2013_aux)
drop if stut_2013<200
drop ies_2013_aux stut_2013

glevelsof codigo_ies, local(ies)
foreach x of local ies{
	sum access_0 if icfes_per==2013 & codigo_ies==`x'
	replace access_0=(access_0/r(mean)-1)*100 if codigo_ies==`x'
	sum pc if icfes_per==2013 & codigo_ies==`x'
	replace pc=(pc/r(mean)-1)*100 if codigo_ies==`x'
}

matrix a=J(18,4,.)
matrix p=J(18,4,.)

local j=1
forvalues i=2010/2015 {
	foreach x in hq_pri hq_pub lq {
		ttest access_0==0 if tipo=="`x'" & icfes_per==`i'
		matrix a[`j',1]==`i'
		matrix a[`j',2]==r(mu_1)
		matrix a[`j',3]==r(mu_1)+1.96*r(se)
		matrix a[`j',4]==r(mu_1)-1.96*r(se)
		
		ttest pc==0 if tipo=="`x'" & icfes_per==`i'
		matrix p[`j',1]==`i'
		matrix p[`j',2]==r(mu_1)
		matrix p[`j',3]==r(mu_1)+1.96*r(se)
		matrix p[`j',4]==r(mu_1)-1.96*r(se)
		local j=`j'+1
	}
}

gen type=""
forvalues i=1(3)18{
	replace type="hq_pri" in `i'
	replace type="hq_pub" in `=`i'+1'
	replace type="lq" in `=`i'+2'
}

svmat a
replace a1=a1-0.1 if type=="hq_pri"
replace a1=a1+0.1 if type=="lq"

#delimit ;
twoway 	(scatter a2 a1 if type=="hq_pri", color(green) m(O))
		(rcap a3 a4 a1 if type=="hq_pri", color(green))
		(scatter a2 a1 if type=="hq_pub", color(blue) m(T))
		(rcap a3 a4 a1 if type=="hq_pub", color(blue))
		(scatter a2 a1 if type=="lq", color(black) m(S))
		(rcap a3 a4 a1 if type=="lq", color(black)),
		legend(order(1 "HQ Private" 3 "HQ Public" 5 "LQ") position(10) ring(0))
		xtitle("Year in which the student took SABER 11", size(small)) ytitle("Percentage change in number of students entering immediately", size(small))
		yline(0, lc(gray) lpattern(dash) lwidth(thin)) xsize(8)
		;
#delimit cr
graph export "${outcomes}/FigA12a.pdf", replace width(20)


svmat p
replace p1=p1-0.1 if type=="hq_pri"
replace p1=p1+0.1 if type=="lq"

#delimit ;
twoway 	(scatter p2 p1 if type=="hq_pri", color(green) m(O))
		(rcap p3 p4 p1 if type=="hq_pri", color(green))
		(scatter p2 p1 if type=="hq_pub", color(blue) m(T))
		(rcap p3 p4 p1 if type=="hq_pub", color(blue))
		(scatter p2 p1 if type=="lq", color(black) m(S))
		(rcap p3 p4 p1 if type=="lq", color(black)),
		legend(order(1 "HQ Private" 3 "HQ Public" 5 "LQ") position(10) ring(0))
		xtitle("Year in which the student took SABER 11", size(small)) ytitle("Percentage change in percentil of immediately entering students", size(small))
		yline(0, lc(gray) lpattern(dash) lwidth(thin)) xsize(8)
		;
#delimit cr
graph export "${outcomes}/FigA12b.pdf", replace width(20)
		
		
